home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / atom / mock_http.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  5.0 KB  |  128 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __author__ = 'api.jscudder (Jeff Scudder)'
  5. import atom.http_interface as atom
  6. import atom.url as atom
  7.  
  8. class Error(Exception):
  9.     pass
  10.  
  11.  
  12. class NoRecordingFound(Error):
  13.     pass
  14.  
  15.  
  16. class MockRequest(object):
  17.     '''Holds parameters of an HTTP request for matching against future requests.
  18.   '''
  19.     
  20.     def __init__(self, operation, url, data = None, headers = None):
  21.         self.operation = operation
  22.         if isinstance(url, (str, unicode)):
  23.             url = atom.url.parse_url(url)
  24.         
  25.         self.url = url
  26.         self.data = data
  27.         self.headers = headers
  28.  
  29.  
  30.  
  31. class MockResponse(atom.http_interface.HttpResponse):
  32.     '''Simulates an httplib.HTTPResponse object.'''
  33.     
  34.     def __init__(self, body = None, status = None, reason = None, headers = None):
  35.         if body and hasattr(body, 'read'):
  36.             self.body = body.read()
  37.         else:
  38.             self.body = body
  39.         if status is not None:
  40.             self.status = int(status)
  41.         else:
  42.             self.status = None
  43.         self.reason = reason
  44.         if not headers:
  45.             pass
  46.         self._headers = { }
  47.  
  48.     
  49.     def read(self):
  50.         return self.body
  51.  
  52.  
  53.  
  54. class MockHttpClient(atom.http_interface.GenericHttpClient):
  55.     
  56.     def __init__(self, headers = None, recordings = None, real_client = None):
  57.         """An HttpClient which responds to request with stored data.
  58.  
  59.     The request-response pairs are stored as tuples in a member list named
  60.     recordings.
  61.  
  62.     The MockHttpClient can be switched from replay mode to record mode by
  63.     setting the real_client member to an instance of an HttpClient which will
  64.     make real HTTP requests and store the server's response in list of 
  65.     recordings.
  66.     
  67.     Args:
  68.       headers: dict containing HTTP headers which should be included in all
  69.           HTTP requests.
  70.       recordings: The initial recordings to be used for responses. This list
  71.           contains tuples in the form: (MockRequest, MockResponse)
  72.       real_client: An HttpClient which will make a real HTTP request. The 
  73.           response will be converted into a MockResponse and stored in 
  74.           recordings.
  75.     """
  76.         if not recordings:
  77.             pass
  78.         self.recordings = []
  79.         self.real_client = real_client
  80.         if not headers:
  81.             pass
  82.         self.headers = { }
  83.  
  84.     
  85.     def add_response(self, response, operation, url, data = None, headers = None):
  86.         '''Adds a request-response pair to the recordings list.
  87.     
  88.     After the recording is added, future matching requests will receive the
  89.     response.
  90.     
  91.     Args:
  92.       response: MockResponse
  93.       operation: str
  94.       url: str
  95.       data: str, Currently the data is ignored when looking for matching
  96.           requests.
  97.       headers: dict of strings: Currently the headers are ignored when
  98.           looking for matching requests.
  99.     '''
  100.         request = MockRequest(operation, url, data = data, headers = headers)
  101.         self.recordings.append((request, response))
  102.  
  103.     
  104.     def request(self, operation, url, data = None, headers = None):
  105.         """Returns a matching MockResponse from the recordings.
  106.     
  107.     If the real_client is set, the request will be passed along and the 
  108.     server's response will be added to the recordings and also returned. 
  109.  
  110.     If there is no match, a NoRecordingFound error will be raised.
  111.     """
  112.         if self.real_client is None:
  113.             if isinstance(url, (str, unicode)):
  114.                 url = atom.url.parse_url(url)
  115.             
  116.             for recording in self.recordings:
  117.                 if recording[0].operation == operation and recording[0].url == url:
  118.                     return recording[1]
  119.             
  120.             raise NoRecordingFound('No recodings found for %s %s' % (operation, url))
  121.         self.real_client is None
  122.         response = self.real_client.request(operation, url, data = data, headers = headers)
  123.         stored_response = MockResponse(body = response, status = response.status, reason = response.reason)
  124.         self.add_response(stored_response, operation, url, data = data, headers = headers)
  125.         return stored_response
  126.  
  127.  
  128.